package com.lz.ionicbase.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lz.ionicbase.R;
import com.lz.ionicbase.SuperApp;
import com.lz.ionicbase.engine.callback.AppCallback;
import com.lz.ionicbase.model.ForgetPsdBean;
import com.lz.ionicbase.utils.BaseUtil;
import com.lz.ionicbase.utils.Constant;
import com.lz.ionicbase.utils.EditTextUtils;
import com.lz.ionicbase.utils.SpUtils;
import com.lz.ionicbase.utils.ToastUtils;
import com.lzy.okgo.OkGo;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by lenovo on 2017/2/20.
 */

public class PsdInputPhoneActivity extends BaseActivity {
    @InjectView(R.id.my_back)
    ImageView myBack;
    @InjectView(R.id.psd_next)
    TextView psdNext;
    @InjectView(R.id.psd_intphone)
    EditText psdIntphone;
    @InjectView(R.id.psd_phone_zone)
    TextView psdPhoneZone;
    private SpUtils sp;
    private String originalPhone;
    private String phoneNumber;
    private String zoneId;

    private Context context;

    @Override
    protected int getActivityLayout() {
        return R.layout.activity_psd_phone;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        EditTextUtils.AddSpaceNumber(this, psdIntphone);
        sp = new SpUtils();
        context = this;
    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.my_back, R.id.psd_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.psd_next:
                String zoneText = (String) psdPhoneZone.getText();
                zoneId = BaseUtil.MatchString("[+]\\d*", zoneText);
                phoneNumber = psdIntphone.getText().toString();
                String userPhone = EditTextUtils.DeleteSpace(phoneNumber);
//                String userPhone = psdIntphone.getText().toString();
                if (!TextUtils.isEmpty(userPhone)) {
                    Boolean isnumber = EditTextUtils.TestPhoneNumber(userPhone);
                    if (isnumber) {
                        originalPhone = zoneId + userPhone;
                        Log.i("------", "originalPhone" + originalPhone);
                        String username = sp.getString(PsdInputPhoneActivity.this, Constant.ACCOUNT, "");
                        String deviceId = sp.getString(PsdInputPhoneActivity.this, Constant.DEVICEID, "");
                        String netUrl = sp.getString(PsdInputPhoneActivity.this, Constant.NET_URL, "");
                        String spLan = SpUtils.getString(getApplicationContext(), SpUtils.KEY_LANGUAGE, "简体中文");
                        String language = (String) SuperApp.lanMap.get(spLan)[0];
                        String base64username = BaseUtil.getBase64(username);
                        String requestUrl = netUrl + Constant.FORGETPSDURL;
                        Log.i("忘记密码Url", requestUrl);
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("loginName", base64username);
                        map.put("uid", deviceId);
                        map.put("reception", originalPhone);
                        map.put("language", language);

                        OkGo.post(requestUrl).tag(this).params(map)
                                .execute(new AppCallback<ForgetPsdBean>(context) {
                                    @Override
                                    public void onSuccess(ForgetPsdBean resObj, Call call, Response response) {
                                        if (resObj.success) {
                                            Intent intent = new Intent(PsdInputPhoneActivity.this, PsdInputCodeActivity.class);
                                            String vcode = resObj.getVcode();
                                            intent.putExtra("vcode", vcode);
                                            intent.putExtra("intoPhone", zoneId + " " + phoneNumber);
                                            startActivity(intent);
                                        } else {
                                            ToastUtils.showToast(PsdInputPhoneActivity.this, "亲，请设置服务器哦", 0);
                                        }
                                    }

                                    @Override
                                    public ForgetPsdBean convertSuccess(Response response) throws Exception {
                                        ForgetPsdBean resObj = new Gson().fromJson(response.body().string(), ForgetPsdBean.class);
                                        return resObj;
                                    }

                                    @Override
                                    public void onError(Call call, Response response, Exception e) {
                                        super.onError(call, response, e);
                                    }
                                });

                    } else {
                        psdIntphone.setTextColor(getResources().getColor(R.color.error));
                        ToastUtils.showToast(PsdInputPhoneActivity.this, "亲，请填写您的手机号有误", 0);
                        psdIntphone.addTextChangedListener(new TextWatcher() {
                            @Override
                            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                            }

                            @Override
                            public void onTextChanged(CharSequence s, int start, int before, int count) {
                                if (start == 0) {
                                    psdIntphone.setTextColor(getResources().getColor(R.color.french_gray));
                                }
                            }

                            @Override
                            public void afterTextChanged(Editable s) {

                            }
                        });
                    }
                } else {
                    ToastUtils.showToast(PsdInputPhoneActivity.this, "亲，请填写您的手机号", 0);
                }
                break;
            case R.id.my_back:
                finish();
                break;
        }
    }
}
